var bigImgNum = 6;  //最大上传图片数量
var imgSrc = []; //图片路径
var imgName = []; //图片名字
var width = [];
var height = [];

$(function () {
    // 鼠标经过显示删除按钮
    $('.content-img-list').on('mouseover', '.content-img-list-item', function () {
        $(this).children('a').removeClass('hide');
    });
    // 鼠标离开隐藏删除按钮
    $('.content-img-list').on('mouseleave', '.content-img-list-item', function () {
        $(this).children('a').addClass('hide');
    });
    // 单个图片删除
    $(".content-img-list").on("click", '.content-img-list-item a', function () {
        var index = $(this).attr("index");
        imgSrc.splice(index, 1);
        width.splice(index, 1);
        height.splice(index, 1);
        imgName.splice(index, 1);
        var boxId = ".content-img-list";
        addNewContent(boxId);
        if (imgSrc.length < bigImgNum) {//显示上传按钮
            $('.content-img .file').show();
        }
    });
    //图片上传
    $('#upload').on('change', function () {

        if (imgSrc.length >= bigImgNum) {
            return alert("最多只能上传" + bigImgNum + "张图片");
        }
        var imgSize = this.files[0].size;  //b
        if (imgSize > 1024 * 1024 * 2) {//2M
            return alert("上传图片不能超过2M");
        }
        console.log(this.files[0].type);
        if (this.files[0].type != 'image/png' && this.files[0].type != 'image/jpeg' && this.files[0].type != 'image/gif') {
            return alert("图片上传格式不正确");
        }
        var imgBox = '.content-img-list';


        //var fileList = this.files;
        // for(var i = 0; i < fileList.length; i++) {
        //     //本地使用
        // 	var imgSrcI = getObjectURL(fileList[i]);
        // 	imgName.push(fileList[i].name);
        // 	imgSrc.push(imgSrcI);
        // 	imgFile.push(fileList[i]);
        // }




        var data = new FormData();
        data.append("file", this.files[0]);
        $.modal.loading("图片上传中，请稍后...");
        $.ajax({
            type: "POST",
            url: ctx + "common/uploadOSSFile",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    imgName.push(result.name);
                    imgSrc.push(result.url);
                    width.push(result.width);
                    height.push(result.height);
                    if (imgSrc.length == bigImgNum) {//隐藏上传按钮
                        $('.content-img .file').hide();
                    }
                    addNewContent(imgBox);
                    this.value = null;//解决无法上传相同图片的问题
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    })
});

//删除
function removeImg(obj, index) {
    imgSrc.splice(index, 1);
    width.splice(index, 1);
    height.splice(index, 1);
    imgName.splice(index, 1);
    var boxId = ".content-img-list";
    addNewContent(boxId);
}

//图片展示
function addNewContent(obj) {
    $(obj).html("");
    for (var a = 0; a < imgSrc.length; a++) {
        var oldBox = $(obj).html();
        var img = $(obj).html(oldBox + '<li class="content-img-list-item"><img src="' + imgSrc[a] + '" alt=""><a index="' + a + '" class="hide delete-btn"><i class="ico-delete"></i></a></li>');
        $(obj).html(img.html()+'<input class="form-control"  name="rtProductRotations['+a+'].roWight" value="'+width[a]+'" type="hidden"/>'
            +'<input class="form-control"  name="rtProductRotations['+a+'].roUrl" value="'+imgSrc[a]+'" type="hidden"/>'
            + '<input class="form-control"  name="rtProductRotations['+a+'].roHight" value="'+height[a]+'" type="hidden"/>');
    }
}

//建立一個可存取到該file的url
function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}